package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    //在service中同时用了service和mapper   确定service能查出来的话就用service
    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    RoomFacilityService roomFacilityService;

    @Autowired
    RoomLabelService roomLabelService;

    @Autowired
    RoomAttrValueService roomAttrValueService;

    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    RoomLeaseTermService roomLeaseTermService;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    RoomFacilityMapper roomFacilityMapper;

    @Autowired
    RoomLabelMapper roomLabelMapper;

    @Autowired
    RoomAttrValueMapper roomAttrValueMapper;

    @Autowired
    RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Autowired
    RoomLeaseTermMapper roomLeaseTermMapper;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        Long roomId = roomSubmitVo.getId();   //继承的话，可以获取其父类的属性，如果set多个属性的话，建议用BeanUtils.copyProperties
        super.saveOrUpdate(roomSubmitVo); //课件中好像没有这个操作
        //不是空的为什么要删除？？
        if (roomId != null) {
            // 更新操作，删除相关数据，这个逻辑要好好理解一下
            // 1 删除图片
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
//            graphInfoQueryWrapper.eq("item_type",ItemType.ROOM);   //这种写法可读性更强一些
            graphInfoQueryWrapper.eq("item_type", 2);
            graphInfoQueryWrapper.eq("item_id", roomId);
            graphInfoService.remove(graphInfoQueryWrapper);
//            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            graphInfoLambdaQueryWrapper.eq(GraphInfo::getId,roomId);
//            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
//            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            // 2 删除房间配套
            QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
            roomFacilityQueryWrapper.eq("room_id", roomId);
            roomFacilityService.remove(roomFacilityQueryWrapper);
            // 3 删除房间标签
            QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
            roomLabelQueryWrapper.eq("room_id", roomId);
            roomLabelService.remove(roomLabelQueryWrapper);
            // 4 删除房间属性值
            QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
            roomAttrValueQueryWrapper.eq("room_id", roomId);
            roomAttrValueService.remove(roomAttrValueQueryWrapper);
//            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomId);
//            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            // 5 删除房间支付方式
            QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq("room_id", roomId);
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);
            // 6 删除房间租期
            QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
            roomLeaseTermQueryWrapper.eq("room_id", roomId);
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);
        }

        this.saveOrUpdate(roomSubmitVo);   //对比公寓的更新代码？？？  相当于？？  roomInfoService.saveOrUpdate()

        // 重新插入新的数据
        //1.保存新的graphInfoList
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }


        //2.保存新的roomAttrValueList
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        //3.保存新的facilityInfoList
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                                                                //为什么新建的时候要设置这两个Id,后续要用到他们么？
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }


        //4.保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        //5.保存新的paymentTypeList
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }



        //6.保存新的leaseTermList
//        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
//        if (!CollectionUtils.isEmpty(leaseTermIds)) {
//            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
//            for (Long leaseTermId : leaseTermIds) {
//                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
//                roomLeaseTerms.add(roomLeaseTerm);
//            }
//            roomLeaseTermService.saveBatch(roomLeaseTerms);
//        }
    }

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        IPage<RoomItemVo> itemVoIPageResult = baseMapper.selectPageItem(roomItemVoPage, queryVo);
        return itemVoIPageResult;
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
         //又是一个转换Vo的过程
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //房间信息  id是谁的就是返回谁的对象
        RoomInfo roomInfo = baseMapper.selectById(id);

        //公寓信息
        Long apartmentId = roomInfo.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);  //将公寓房间相关的属性复制批量复制到到roomDetailVo中
        roomDetailVo.setApartmentInfo(apartmentInfo);

        //图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        List<GraphInfo> graphInfos = graphInfoService.list(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = graphInfos.stream().map(
                graphInfo -> {
                    GraphVo graphVo = new GraphVo();
                    BeanUtils.copyProperties(graphInfo, graphVo);
                    return graphVo;
                }
        ).collect(Collectors.toList());

        //配套
        //这些实体类在哪里能体现出来了，熟悉数据结构。
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectListByRoomId(id);
        System.out.println("facilityInfoList = " + facilityInfoList);
        //标签
        List<LabelInfo> labelInfoList = roomLabelMapper.selectListByRoomId(id);
        //属性
        List<AttrValueVo> attrValueVoList = roomAttrValueMapper.selectListByRoomId(id);

        //支付方式
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.selectListByRoomId(id);

        //租期
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.selectListByRoomId(id);

        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;


//        RoomDetailVo roomDetailVo = new RoomDetailVo();
//        //公寓信息
////        RoomInfo roomInfo1 = roomInfoMapper.selectById(id);//可读性更高
//        RoomInfo roomInfo = baseMapper.selectById(id);
//        Long apartmentId = roomInfo.getApartmentId();
//        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);
//
//        //图片信息
//        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
//        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
//        List<GraphInfo> graphInfos = graphInfoService.list(graphInfoLambdaQueryWrapper);
//        List<GraphVo> graphVoList = graphInfos.stream().map(
//                graphInfo -> {
//                    GraphVo graphVo = new GraphVo();
//                    BeanUtils.copyProperties(graphInfo, graphVo);
//                    return graphVo;
//                }
//        ).collect(Collectors.toList());
//        //配套信息
//        List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectListByRoomId(id);
//        //标签信息
//        List<LabelInfo> labelInfoList = roomLabelMapper.selectListByRoomId(id);
//        //属性信息
//        List<AttrValueVo> attrValueVos = roomAttrValueMapper.selectListByRoomId(id);
//        //支付方式
//        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.selectListByRoomId(id);
//        //租期信息
//        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.selectListByRoomId(id);
//        BeanUtils.copyProperties(roomInfo,roomDetailVo);
//        roomDetailVo.setApartmentInfo(apartmentInfo);
//        roomDetailVo.setGraphVoList(graphVoList);
//        roomDetailVo.setAttrValueVoList(attrValueVos);
//        roomDetailVo.setFacilityInfoList(facilityInfoList);
//        roomDetailVo.setLabelInfoList(labelInfoList);
//        roomDetailVo.setPaymentTypeList(paymentTypeList);
//        roomDetailVo.setLeaseTermList(leaseTermList);
//        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {

        //1.删除graphInfoList
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM); //你怎么知道它是枚举？？
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getId, id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //2.删除attrValueList
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        //3.删除配套信息
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

        //4.删除标签信息
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        //5.删除支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        //6.删除租期信息
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
//        roomLeaseTermMapper.delete(roomLeaseTermLambdaQueryWrapper);

        //7.删除公寓信息
        apartmentInfoMapper.deleteById(id);
        //graphInfoMapper.deleteById(id); //为什么不用mapper?? (应该都可以吧？？只是要再注入对应的Mapper)

    }

    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Override
    public List<Long> getReleaseRoomByApartmentId(Long apartmentId) {
        List<Long> leaseRoomIdList = roomInfoMapper.selectReleaseRoomByApartmentId(apartmentId);
        return leaseRoomIdList;
    }
}




